package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.wx.vo.CurrentCategoryVO;
import com.cskaoyan.wx.vo.WxCatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class WxCatalogServiceImpl implements WxCatalogService {
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 分类首页
     * @return
     */
    @Override
    public WxCatalogVO getCatalogIndex() {
        WxCatalogVO wxCatalogVO = new WxCatalogVO();

        //当前第一级类别
        Category category = categoryMapper.selectFirstCatalog();
        Integer id = category.getId();
        wxCatalogVO.setCurrentCategory(category);

        //第一级类别
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        criteria.andPidEqualTo(0);
        criteria.andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        wxCatalogVO.setCategoryList(categories);

        //当前第一级累的第二季类别
        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
        criteria1.andDeletedNotEqualTo(true);
        criteria1.andPidEqualTo(id);
        criteria1.andLevelEqualTo("L2");
        List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
        wxCatalogVO.setCurrentSubCategory(categories1);

        return wxCatalogVO;
    }

    /**
     * 选择的当前分类
     * @param id
     * @return
     */
    @Override
    public CurrentCategoryVO getCurrentCatalog(Integer id) {
        CurrentCategoryVO currentCategoryVO = new CurrentCategoryVO();

        //当前第一级类
        Category category = categoryMapper.selectByPrimaryKey(id);
        currentCategoryVO.setCurrentCategory(category);

        //对应第二季类别
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        criteria.andPidEqualTo(id);
        criteria.andLevelEqualTo("L2");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        currentCategoryVO.setCurrentSubCategory(categories);

        return currentCategoryVO;
    }
}
